<?php
/**
 * Title: HTML Table generator
 * Description: Generates XHTML 1.0 Strict tables
 * @requires The LinkedList package. The packages is used to manges rows and table cells
 * @author Oddleif Halvorsen | leif-h@online.no
 * @version 1.0
 */

include_once("HTMLTag.php");
include_once("Tr.php");
include_once("Td.php");
include_once("Th.php");

class Table extends HTMLTag{
	//the list of rows
	var $rows;
	
	//table tag attributes
	var $border;
	var $width;
	var $cellspacing;
	var $cellpadding;
	var $frame;
	var $rules;
	var $summary;
	
	//some other nessesary attributes
	var $tableHeaderIsSet; //bool value to check if the header is set or not.
	
	function Table(){
		parent::HTMLTag();
		
		$this->rows = array();
		$this->tableHeaderIsSet = FALSE;
	}
	
	function setBorder($border){ $this->border = $border;	}
	function setCellpadding($padding){ $this->cellpadding = $padding; }
	function setCellSpacing($spacing){ $this->cellspacing = $spacing;	}
	function setFrame($frame){ $this->frame = $frame;	}
	function setRules($rules){ $this->rules = $rules;	}
	function setSummary($summary){ $this->summary = $summary;	}
	
	/**
	 * Sets the with for the table.
	 * @param $with A numeric value
	 * @param $mesurement px || %
	 */
	function setWidth($with, $mesurement = "px"){
		$this->width = ($with . $mesurement);
	}
	
	function getBorder(){	return (isset($this->border) ? $this->border : FALSE); }
	function getCellpadding(){ return (isset($this->cellpadding) ? $this->cellpadding : FALSE);	}
	function getCellSpacing(){ return (isset($this->cellspacing) ? $this->cellspacing : FALSE);	}
	function getFrame(){ return (isset($this->frame) ? $this-frame : FALSE); }
	function getRules(){ return (isset($this->frame) ? $this->rules : FALSE);	}
	function getSummary(){ return (isset($this->frame) ? $this->summary : FALSE);	}
	function getWidth(){ return (isset($this->width) ? $this->width : FALSE);	}
	
	/**
	 * Adds a new row to the end of the table
	 * @param $row A Row object
	 */
	function addRow($row){
		array_push($this->rows, $row);
	}
	
	/**
	 * Sets the table header. 
	 * Uses add if the list is empty, addAtPosition does not work at a empty list.
	 * @param $tableHeader A row object with the header
	 */
	function setTableHeader($tableHeader){
		if(!$this->tableHeaderIsSet){
			if(!count($this->rows))
				array_splice($this->rows, 0, count($this->rows), array_merge(array($tableHeader), array_slice($this->rows, 0)));
			else
				array_push($this->rows, $tableHeader);
				
			$this->tableHeaderIsSet = TRUE;
		}
		else
			return FALSE;
	}
	
	/**
	 * Sets the column headers for the table. Inserts the
	 * coulmn headers on row 0 og row 1, depending on wheter the 
	 * table header is set.
	 * @param $tableHeader A row object with the header
	 */	
	function setColumnHeaders($columnHeaders){
		$rowCount = count($this->rows);
		if(!$rowCount && $rowCount>1){
			$columnHeaderRowNum = ($this->tableHeaderIsSet ? 1 : 0);
			array_splice($this->rows, columnHeaderRowNum, count($this->rows),
				array_merge(array($columnHeaders), array_slice($this->rows, columnHeaderRowNum)));
		}
		else
			array_push($this->rows, $columnHeaders);
	}
	
	/**
	 * Return the html code for the table
	 * @return The html code for the table
	 */
	function getHtml(){
		$table = &$this->setTableAttributes();
				
		//inserts the table rows
		foreach($this->rows as $row)
		{
			$table .= $row->getHtml();
		}

		$table .= "</table>";
		return $table;
	}
	
	/**
	 * Sets the attributes for the table
	 * @return A table tag with attributes
	 */
	function setTableAttributes(){
		$table = "<table";
		
		//gets the parents attributes
		if(parent::getAttributes() != FALSE)
			$table .= parent::getAttributes();
		
		if($this->getBorder())
			$table .=  (" border=\"" . $this->getBorder() . "\"");
		if($this->getCellpadding())
			$table .=  (" cellpadding=\"" . $this->getCellpadding() . "\"");
		if($this->getCellSpacing())
			$table .=  (" cellspacing=\"" . $this->getCellSpacing() . "\"");
		if($this->getFrame())
			$table .=  (" frame=\"" . $this->getFrame() . "\"");
		if($this->getRules())
			$table .=  (" rules=\"" . $this->getRules() . "\"");
		if($this->getSummary())
			$table .=  (" summary=\"" . $this->getSummary() . "\"");
		if($this->getWidth())
			$table .=  (" width=\"" . $this->getWidth() . "\"");
			
		$table .= ">";
		
		return $table;
	}
}
?>